﻿using GC_FFW.BusinessEntities.Abstractions.Interfaces.Forecast;
using GC_FFW.BusinessEntities.Forecast.Xml;
using GC_FFW.ForecastWriter.Abstractions.Adapters;
using GC_FFW.ForecastWriter.Abstractions.Interfaces.Helpers;
using GC_FFW.ForecastWriter.Common;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace GC_FFW.ForecastWriter.Adapters.TeamMatch.Prognosis
{
	public class WinCoefficientProposalAdapter : ProposalBaseAdapter
	{
		#region Constructors

		public WinCoefficientProposalAdapter(IForecastData forecastData, Conditions conditions,
			IStringManager stringManager, IRandomizer randomizer)
			: base(forecastData, conditions, stringManager, randomizer) { }

		#endregion

		#region Overrides

		public override string GetText()
		{
			if (ForecastData.Coefficients == null || ForecastData.Coefficients.TeamWinner == null)
				return string.Empty;

			Proposal proposal;

			if (ForecastData.Coefficients.TeamWinner.IsEqualCoefficients)
			{
				proposal = GetProposal(Conditions.GetCondition(3));
				return proposal != null && proposal.Text != null ? proposal.Text : string.Empty;
			}

			proposal = GetProposal(Conditions.GetCondition(ForecastData.Coefficients.TeamWinner.Min <= 1.4 ? 1 : 2));
			if (proposal == null) return string.Empty;

			var result = proposal.Text ?? string.Empty;
			if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

			var dictionary = new Dictionary<string, string>();
			foreach (var keyword in proposal.KeywordList)
			{
				switch (keyword)
				{
					case Constants.WINNING_COEFFICIENT_KEYWORD:

						dictionary.Add(
							Constants.WINNING_COEFFICIENT_KEYWORD,
							ForecastData.Coefficients.TeamWinner.Min.ToString(CultureInfo.InvariantCulture));

						break;
				}
			}

			result = StringManager.ReplaceKeywords(result, dictionary);

			return result;
		}

		#endregion
	}
}
